package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.InfDisciplines;
import com.ctcemti.mdm.project.sys.service.InfDisciplinesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SysMenuController
 * @Description 专业领域控制层
 * @Author zpw
 * @Date 11:50 2019/7/16
 * @Version V1.0
 **/
@RestController
@Api(value = "InfDisciplinesController", description = "专业领域控制器类")
@RequestMapping("infDisciplines")
public class InfDisciplinesController {

    @Autowired
    private InfDisciplinesService infDisciplinesService;

    @ApiOperation(value = "获取所有的领域(一级树结构，不分页)")
    @PostMapping("selectAllDisciplinesByTree")
    @ApiImplicitParams({
            @ApiImplicitParam(value="type",name = "类型"),
            @ApiImplicitParam(value="name",name = "名称")
    })
    public Result<InfDisciplines> selectAllDisciplinesByTree(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        List<InfDisciplines> list = infDisciplinesService.selectAllMenu(infDisciplines);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,list);
    }


    @ApiOperation(value = "获取所有的专业领域(二级树结构，不分页)")
    @PostMapping("selectAllInfDisciplinesByTree")
    @ApiImplicitParams({
            @ApiImplicitParam(value="name",name = "名称")
    })
    public Result<InfDisciplines> selectAllInfDisciplinesByTree(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        List<InfDisciplines> list=null;
        if(infDisciplines.getName()!=null&&infDisciplines.getName()!=""){
            infDisciplines.setName("%"+infDisciplines.getName()+"%");
            list = infDisciplinesService.selectAllInfDisciplinesByTree(infDisciplines);
        }else{
            list = infDisciplinesService.selectAllMenu(new InfDisciplines());
        }
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "获取所有的领域(分页)")
    @PostMapping("selectAllDisciplines")
    @ApiImplicitParams({
            @ApiImplicitParam(value="pid",name = "父级id"),
            @ApiImplicitParam(value="type",name = "类型")
    })
    public Result<InfDisciplines> selectAllDisciplines(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        PageHelper.startPage(infDisciplines.getPageIndex(), infDisciplines.getPageSize(),"sort");
        List<InfDisciplines> list = infDisciplinesService.selectAllMenu(infDisciplines);
        PageInfo<InfDisciplines> pagelist = new PageInfo<InfDisciplines>(list);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,pagelist);
    }

    @ApiOperation(value = "新增专业、领域")
    @PostMapping("insertInfDisciplines")
    @ApiImplicitParam(value="InfDisciplines",name = "专业领域实体类")
    public Result<InfDisciplines> insertInfDisciplines(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDisciplines.setCreator(loginId);
        infDisciplinesService.insertInfDisciplines(infDisciplines);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "根据id查询实体")
    @PostMapping("queryInfDisciplinesByID")
    @ApiImplicitParam(value="id",name = "专业领域ID")
    public Result<InfDisciplines> queryInfDisciplinesByID(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        InfDisciplines queryInfDisciplines = infDisciplinesService.queryInfDisciplinesByID(infDisciplines);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,queryInfDisciplines);
    }

    @ApiOperation(value = "根据条件查询列表")
    @PostMapping("queryInfDisciplinesList")
    @ApiImplicitParam(value="InfDisciplines",name = "专业领域实体类")
    public Result<InfDisciplines> queryInfDisciplinesList(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        List<InfDisciplines> queryInfDisciplines = infDisciplinesService.queryInfDisciplinesList(infDisciplines);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,queryInfDisciplines);
    }
    @ApiOperation(value = "修改")
    @PostMapping("updateInfDisciplines")
    @ApiImplicitParam(value="InfDisciplines",name = "专业领域实体类")
    public Result<InfDisciplines> updateInfDisciplines(InfDisciplines infDisciplines,HttpServletRequest request, HttpServletResponse response){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        infDisciplines.setModifiedBy(loginId);
        infDisciplines.setModifyDate(new Date());
        int queryInfDisciplines = infDisciplinesService.updateInfDisciplines(infDisciplines);
        return new Result<InfDisciplines>(ResultEnum.SUCCESS,queryInfDisciplines);
    }
    @ApiOperation(value = "批量删除")
    @PostMapping("deleteInfDisciplines")
    @ApiImplicitParam(value="id",name = "专业领域ID")
    public Result<InfDisciplines> deleteInfDisciplines(  Integer[]  ids,Integer type
            , HttpServletRequest request, HttpServletResponse response){
        return  infDisciplinesService.deleteInfDisciplines(ids,type);
    }
}
